package com.nus;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;



public class FindMeInMap extends MapActivity {
	protected TextView title;
	 protected boolean isRouteDisplayed() {
	    return false;
	  }

//	 public  MapController mapController;
//	  public MyLocationOverlay positionOverlay;
	 MapController mc;
	    GeoPoint p;
	
	  public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		  setContentView(R.layout.gothere);
			 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
			 title = (TextView) findViewById(R.id.titleheader);
			 title.setText("NUS Navigator");
		  
//		  Intent streetView = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.streetview:cbll="+1.296394+","+103.772832));
//		  startActivity(streetView);
//	  
			 ImageView b = (ImageView)findViewById(R.id.home);
			  b.setImageDrawable(getResources().getDrawable(R.drawable.maps));
	    MapView mapView = (MapView)findViewById(R.id.theMapView);
	 
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 
 
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);
        mapView.setStreetView(false);
        mapView.setSatellite(true);
        mc = mapView.getController();
        Bundle extras = getIntent().getExtras();
		
			
		
        String coordinates[] = {extras.getString("lat"), extras.getString("long")};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
 
        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
 
        mc.animateTo(p);
        mc.setZoom(19); 
        mapView.invalidate();
	    mapView.displayZoomControls(true);
	    mapView.setBuiltInZoomControls(true);
	    MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);  
	  
	  } 
	  class MapOverlay extends com.google.android.maps.Overlay
	    {
	        @Override
	        public boolean draw(Canvas canvas, MapView mapView, 
	        boolean shadow, long when) 
	        {
	            super.draw(canvas, mapView, shadow);                   
	 
	            //---translate the GeoPoint to screen pixels---
	            Point screenPts = new Point();
	            mapView.getProjection().toPixels(p, screenPts);
	 
	            //---add the marker---
	            Bitmap bmp = BitmapFactory.decodeResource(
	                getResources(), R.drawable.map_marker_icon);            
	            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
	            return true;
	        }
	    } 
	 

//
//	    // Add the MyPositionOverlay
//	    positionOverlay = new MyLocationOverlay(getApplicationContext(),myMapView);
//	    List<Overlay> overlays = myMapView.getOverlays();
//	    overlays.add(positionOverlay);
//
//	    LocationManager locationManager;
//	    String context = Context.LOCATION_SERVICE;
//	    locationManager = (LocationManager)getSystemService(context);
//
//	    Criteria criteria = new Criteria();
//	    criteria.setAccuracy(Criteria.ACCURACY_FINE);
//	    criteria.setAltitudeRequired(false);
//	    criteria.setBearingRequired(false);
//	    criteria.setCostAllowed(true);
//	    criteria.setPowerRequirement(Criteria.POWER_LOW);
//	    String provider = locationManager.getBestProvider(criteria, true);
//
//	    Location location = locationManager.getLastKnownLocation(provider);
//
//	    updateWithNewLocation(location);
//
//	    locationManager.requestLocationUpdates(provider, 2000, 10,   
//	                                           locationListener);
//	  }
	  
	  
	

//	  private final LocationListener locationListener = new LocationListener() {
//	    public void onLocationChanged(Location location) {
//	      updateWithNewLocation(location);
//	    }
//
//	    public void onProviderDisabled(String provider){
//	      updateWithNewLocation(null);
//	    }
//
//	    public void onProviderEnabled(String provider){ }
//	    public void onStatusChanged(String provider, int status, 
//	                                Bundle extras){ }
//	  };

//	  private void updateWithNewLocation(Location location) {
//	    String latLongString;
//	    TextView myLocationText;
//	    myLocationText = (TextView)findViewById(R.id.myLocationText);
//	    String addressString = "No address found";
//
//	    if (location != null) {
//	         // Update my location marker
////	        ((Object) positionOverlay).setLocation(location);
//
//	      // Update the map location.
//	      Double geoLat = location.getLatitude()*1E6;
//	      Double geoLng = location.getLongitude()*1E6;
//	      GeoPoint point = new GeoPoint(geoLat.intValue(), 
//	                                    geoLng.intValue());
//
//	      mapController.animateTo(point);
//	      mapController.setZoom(17);
//
//
//	      double lat = location.getLatitude();
//	      double lng = location.getLongitude();
//	      latLongString = "Lat:" + lat + "\nLong:" + lng;
//
//	      double latitude = location.getLatitude();
//	      double longitude = location.getLongitude();
//
//	      Geocoder gc = new Geocoder(this, Locale.getDefault());
//	      try {
//	        List<Address> addresses = gc.getFromLocation(latitude, 
//	                                                     longitude, 1);
//	        StringBuilder sb = new StringBuilder();
//	        if (addresses.size() > 0) {
//	          Address address = addresses.get(0);
//
//	          for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
//	            sb.append(address.getAddressLine(i)).append("\n");
//
//	            sb.append(address.getLocality()).append("\n");
//	            sb.append(address.getPostalCode()).append("\n");
//	            sb.append(address.getCountryName());
//	        }
//	        addressString = sb.toString();
//	      } catch (IOException e) {}
//	    } else {
//	      latLongString = "No location found";
//	    }
//	    myLocationText.setText("Your Current Position is:\n" + 
//	                           latLongString + "\n" + addressString);
//	  }
	}